var Manager = _p.Model.extend({
    defaults: {
        tasks: []
    },

    dataClassesMap: {
        tasks: Task
    },
/*
    fixTasks: function (){
        for (var i=0; i<this.data.tasks[i]; i++) {
            var task = this.data.tasks[i]
        }
    },
*/
    registerTask: function(task) {
        task.subscribe(_p.utils.bind(this.onTaskEvent, this, task));
    },

    onTaskEvent: function(task, eventName, info) {
        switch(eventName) {
            case "status-changed":
                this.fireEvent("statistics-changed");
                this.fireEvent("changed");
                break;
            case "removed":
                for (var i=0; i<this.data.tasks.length; i++) {
                    if (this.data.tasks[i].data.id == task.data.id) {
                        this.data.tasks.splice(i, 1);
                        this.fireEvent("statistics-changed");
                        break;
                    }
                }
                this.fireEvent("changed");
                break;
        }
    },

    generateNewTaskId: function() {
        return this.data.tasks.length ? 
            (Number(this.data.tasks[this.data.tasks.length - 1].data.id) + 1) : 1;
    },

    onViewEvent: function(view, eventName, info) {
        switch(eventName) {
            case "add-task-requested":
                var data = $.extend({
                    id: this.generateNewTaskId()
                }, info);
                var task = new Task(data);
                this.data.tasks.push(task);
                this.registerTask(task);
                this.fireEvent("task-added", {task: task});
                this.fireEvent("statistics-changed");
                this.fireEvent("changed");
                break;
            case "delete-completed-requested":
                for (var i=this.data.tasks.length - 1; i>=0; i--) {
                    var task = this.data.tasks[i];
                    if (task.data.done) {
                        this.data.tasks.splice(i, 1);
                        task.destroyView();
                        this.fireEvent("statistics-changed");
                    }
                }
                this.fireEvent("changed");
                break;
            default:
                return this._superCall();
        }
    },

    onOwnEvent: function(eventName, info) {
        switch(eventName) {
            case "changed":
                var key = this.getProxy("storage").getSavingKey();
                this.getProxy("storage").save();
                break;
            case "status-changed":
                if ("loading-success" == info.reason) {
                    for (var i=0; i<this.data.tasks.length; i++) {
                        this.registerTask(this.data.tasks[i]);
                    }
                }
                break;
        }
    },

    satelliteClasses: {
        View: {
            template: $("#template>.manager"),

            render: function() {
                this._superCall();

                for (var i=0; i<this.modelData.tasks.length; i++) {
                    var task = this.modelData.tasks[i];
                    this.renderTask(task);
                }
                this.renderStatistics();
                
                var self = this;
                $("input.add", this.el).keydown(function(event) {
                    if (13 == event.keyCode) {
                        self.modelCallback("add-task-requested", {
                            title: $(this).attr("value")
                        });
                        $(this).attr("value", "");
                    }
                });
                $(".delete-completed", this.el).click(function(event) {
                    self.modelCallback("delete-completed-requested");
                });
            },

            renderTask: function(task) {
                $(".tasks", this.el).append(task.getView().getEl());
            },

            renderStatistics: function() {
                this.modelData.tasks.length ?
                    $(".statistics", this.el).removeClass("none") :
                    $(".statistics", this.el).addClass("none");
                
                var completedTasksCount = 0;
                for (var i=0; i<this.modelData.tasks.length; i++) {
                    var task = this.modelData.tasks[i];
                    if (task.data.done) {
                        completedTasksCount++;
                    }
                }

                completedTasksCount ?
                    $(".statistics", this.el).addClass("has-completed") :
                    $(".statistics", this.el).removeClass("has-completed");

                $(".delete-completed .value", this.el).text(completedTasksCount);
                $(".open-tasks .value", this.el).text(this.modelData.tasks.length - completedTasksCount);
            },

            onModelEvent: function(eventName, info) {
                switch(eventName) {
                    case "task-added":
                        this.renderTask(info.task);
                        break;
                    case "statistics-changed":
                        this.renderStatistics();
                        break;
                    default:
                        return this._superCall();
                        break;
                }
            }
        },
        ProxyStorage: {
            getLoadingKey: function() {
                return "proton-todo-list";
            },
            getSavingKey: function() {
                return "proton-todo-list";
            }
        }
    }
});